package com.nave.view;

import nave.control.Options;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;

import com.nave.R;
import com.nave.utils.SoundController;

public class OptionsActivity extends Activity
{
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);

		setContentView(R.layout.options);

		buttonSetup();
		
		if ( Options.sound() && !MainMenu.mediaPlayer.isPlaying() ) MainMenu.mediaPlayer.start();
	}

	private void playClickSound()
    {
    	SoundController.getReference().
			playSound(SoundController.CLICK, getApplicationContext());
    }

	private void buttonSetup()
	{	
		Button backButton = (Button) findViewById(R.id.options_back);
		
		RadioButton lives[] = new RadioButton[]
				{
				(RadioButton) findViewById(R.id.lives1),
				(RadioButton) findViewById(R.id.lives2),
				(RadioButton) findViewById(R.id.lives3),
				(RadioButton) findViewById(R.id.lives4),
				(RadioButton) findViewById(R.id.lives5)
				};

		lives[ Options.getLives() - 1].setChecked(true);
		
		RadioButton easy = (RadioButton) findViewById(R.id.easy);
		RadioButton medium = (RadioButton) findViewById(R.id.medium);
		RadioButton hard = (RadioButton) findViewById(R.id.hard);
		
		switch ( Options.getDifficulty() )
		{
		case Options.EASY:
			easy.setChecked(true);
			break;
		case Options.MEDIUM:
			medium.setChecked(true);
			break;
		case Options.HARD:
			hard.setChecked(true);
			break;
		}

		CheckBox sound = (CheckBox) findViewById(R.id.sound);
		sound.setChecked( Options.sound() );


		for (int i = 0; i < lives.length; i++)
		{
			final int temp = i + 1;
			lives[i].setOnClickListener(new OnClickListener()
			{
				@Override
				public void onClick(View v)
				{
					playClickSound();
					Options.setLives(temp);
				}
			});
		}
		
		easy.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View arg0)
			{
				playClickSound();
				Options.setDifficulty(Options.EASY);
			}
		});
		medium.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View arg0)
			{
				playClickSound();
				Options.setDifficulty(Options.MEDIUM);
			}
		});
		hard.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View arg0)
			{
				playClickSound();
				Options.setDifficulty(Options.HARD);
			}
		});
		
		sound.setOnCheckedChangeListener(new OnCheckedChangeListener()
		{
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
			{
				Options.invertSound();
				playClickSound();

				if ( Options.sound() )
				{
					if ( !MainMenu.mediaPlayer.isPlaying() )
						MainMenu.mediaPlayer.start();
				}
				else
				{
					if ( MainMenu.mediaPlayer.isPlaying() )
						MainMenu.mediaPlayer.pause();
				}
					
			}
		}) ; 
		
        backButton.setOnClickListener(new Button.OnClickListener()
        {
			@Override
			public void onClick(View v)
			{
				playClickSound();
				OptionsActivity.this.finish();
			}
		});
	}
}
